﻿#include <QFile>
#include <QJsonDocument>
#include "ConfigHelper.h"


ConfigHelper::ConfigHelper(QString const& fileName,
	QObject* parent /*= nullptr*/)
	: QObject(parent)
	, m_fileName(fileName) {
	try
	{
		reload();
	}
	catch (...) {
		std::exception_ptr eptr = std::current_exception();
		std::rethrow_exception(eptr);
	}	
}

ConfigHelper::~ConfigHelper() {

}

QJsonObject ConfigHelper::rootObject() const {
	return m_root;
}

double ConfigHelper::m1413Speed() const {
	return m_root["view3dAnimation"]["m1413Speed"].toDouble();
}

void ConfigHelper::setM1413Speed(double value) {
	if (m1413Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m1413Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m1413SpeedChanged();
	}
}

double ConfigHelper::enc3Min() const {
	return m_root["encoder"]["enc3"]["min"].toDouble();
}

void ConfigHelper::setEnc3Min(double value) {
	if (enc3Min() != value) {
		QJsonObject encoderObj = m_root["encoder"].toObject();
		QJsonObject enc3 = encoderObj["enc3"].toObject();
		enc3["min"] = value;
		encoderObj["enc3"] = enc3;
		m_root["encoder"] = encoderObj;
		emit enc3MinChanged();
	}
}

double ConfigHelper::enc3Max() const {
	return m_root["encoder"]["enc3"]["max"].toDouble();
}

void ConfigHelper::setEnc3Max(double value) {
	if (enc3Max() != value) {
		QJsonObject encoderObj = m_root["encoder"].toObject();
		QJsonObject enc3 = encoderObj["enc3"].toObject();
		enc3["max"] = value;
		encoderObj["enc3"] = enc3;
		m_root["encoder"] = encoderObj;
		emit enc3MaxChanged();
	}
}

int ConfigHelper::firstScreen() const {
	return m_root["firstScreen"].toInt();
}

int ConfigHelper::secondScreen() const {
	return m_root["secondScreen"].toInt();
} 

double ConfigHelper::m5Speed() const {
	return m_root["view3dAnimation"]["m5Speed"].toDouble();
}

void ConfigHelper::setM5Speed(double value) {
	if (m5Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m5Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m5SpeedChanged();
	}
}

double ConfigHelper::m6Speed() const {
	return m_root["view3dAnimation"]["m6Speed"].toDouble();
}

void ConfigHelper::setM6Speed(double value) {
	if (m6Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m6Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m6SpeedChanged();
	}
}

double ConfigHelper::m4RotSpeed() const {
	return m_root["view3dAnimation"]["m4RotSpeed"].toDouble();
}

void ConfigHelper::setM4RotSpeed(double value) {
	if (m4RotSpeed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m4RotSpeed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m4RotSpeedChanged();
	}
}

double ConfigHelper::m7Speed() const {
	return m_root["view3dAnimation"]["m7Speed"].toDouble();
}

void ConfigHelper::setM7Speed(double value) {
	if (m7Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m7Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m7SpeedChanged();
	}
}

double ConfigHelper::m2Speed() const {
	return m_root["view3dAnimation"]["m2Speed"].toDouble();
}

void ConfigHelper::setM2Speed(double value) {
	if (m2Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m2Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m2SpeedChanged();
	}
}

double ConfigHelper::m3RotSpeed() const {
	return m_root["view3dAnimation"]["m3RotSpeed"].toDouble();
}

void ConfigHelper::setM3RotSpeed(double value) {
	if (m3RotSpeed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m3RotSpeed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m3RotSpeedChanged();
	}
}

double ConfigHelper::m11Speed() const {
	return m_root["view3dAnimation"]["m11Speed"].toDouble();
}

void ConfigHelper::setM11Speed(double value) {
	if (m11Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m11Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m11SpeedChanged();
	}
}

double ConfigHelper::m1RotSpeed() const {
	return m_root["view3dAnimation"]["m1RotSpeed"].toDouble();
}

void ConfigHelper::setM1RotSpeed(double value) {
	if (m1RotSpeed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m1RotSpeed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m1RotSpeedChanged();
	}
}

double ConfigHelper::m13Speed() const {
	return m_root["view3dAnimation"]["m13Speed"].toDouble();
}

void ConfigHelper::setM13Speed(double value) {
	if (m13Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m13Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m13SpeedChanged();
	}
}

double ConfigHelper::m14Speed() const {
	return m_root["view3dAnimation"]["m14Speed"].toDouble();
}

void ConfigHelper::setM14Speed(double value) {
	if (m14Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m14Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m14SpeedChanged();
	}
}

double ConfigHelper::m12Speed() const {
	return m_root["view3dAnimation"]["m12Speed"].toDouble();
}

void ConfigHelper::setM12Speed(double value) {
	if (m12Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m12Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m12SpeedChanged();
	}
}

double ConfigHelper::m08Speed() const {
	return m_root["view3dAnimation"]["m08Speed"].toDouble();
}

void ConfigHelper::setM08Speed(double value) {
	if (m08Speed() != value) {
		auto&& view3dObj = m_root["view3dAnimation"].toObject();
		view3dObj["m08Speed"] = value;
		m_root["view3dAnimation"] = view3dObj;
		emit m08SpeedChanged();
	}
}

double ConfigHelper::enc2Min() const {
	return m_root["encoder"]["enc2"]["min"].toDouble();
}

void ConfigHelper::setEnc2Min(double value) {
	if (enc2Min() != value) {
		QJsonObject encoderObj = m_root["encoder"].toObject();
		QJsonObject enc2 = encoderObj["enc2"].toObject();
		enc2["min"] = value;
		encoderObj["enc2"] = enc2;
		m_root["encoder"] = encoderObj;
		emit enc2MinChanged();
	}
}

double ConfigHelper::enc2Max() const {
	return m_root["encoder"]["enc2"]["max"].toDouble();
}

void ConfigHelper::setEnc2Max(double value) {
	if(enc2Max() != value) {
		QJsonObject encoderObj = m_root["encoder"].toObject();
		QJsonObject enc2 = encoderObj["enc2"].toObject();
		enc2["max"] = value;
		encoderObj["enc2"] = enc2;
		m_root["encoder"] = encoderObj;
		emit enc2MaxChanged();
	}
}

void ConfigHelper::reload() {
	QFile file(m_fileName);
	if (!file.open(QIODevice::ReadOnly)) {
		std::throw_with_nested(std::logic_error(QStringLiteral("config file read error: %1")
			.arg(file.errorString())
			.toStdString()));
	}
	QJsonParseError error;
	QJsonDocument doc = QJsonDocument::fromJson(file.readAll(),
		&error);
	if (error.error != QJsonParseError::NoError) {
		std::throw_with_nested(std::logic_error("config file is not json format!"));
	}
	m_root = doc.object();
}

bool ConfigHelper::updateConfig(QJsonObject const& obj) {
	QFile file(m_fileName);
	if (!file.open(QIODevice::WriteOnly | QIODeviceBase::Truncate)) {
		return false;
	}
	QJsonDocument doc(obj);
	file.write(doc.toJson());
	file.close();
	return true;
}
